package demoMod.stsconsole.commands;

import demoMod.stsconsole.ConsoleEntry;
import demoMod.stsconsole.enums.ErrType;
import demoMod.stsconsole.helpers.PromptHelper;
import demoMod.stsconsole.interfaces.BackPromptSubscriber;

import java.util.Scanner;

public class Back extends AbstractCommand {
    @Override
    public void execute(String[] args, Scanner sc) {
        if (args.length > 1) {
            this.errMsg(ErrType.WRONG_ARGUMENT);
            return;
        }
        int times = 1;
        try {
            if (args.length == 1) times = Math.max(Integer.parseInt(args[0]), 1);
        } catch (NumberFormatException e) {
            this.errMsg(ErrType.WRONG_ARGUMENT);
            return;
        }
        if (times > 16) {
            this.errMsg(ErrType.WRONG_ARGUMENT);
            return;
        }
        for (int i=0;i<times;i++) {
            PromptHelper.backwardPrompt();
        }
        ConsoleEntry.triggerSubscribers(BackPromptSubscriber.class, "onBackPrompt");
    }

    @Override
    public boolean requiredPrompt(String currentPrompt) {
        return true;
    }
}
